package com.scnu.yxp.travelapp.homepage;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.scnu.yxp.travelapp.R;
import com.scnu.yxp.travelapp.adapter.HomePageListViewAdapter;
import com.scnu.yxp.travelapp.me.MyCollection;
import com.scnu.yxp.travelapp.me.MyDiary;
import com.scnu.yxp.travelapp.newedit.EditDairy;
import com.scnu.yxp.travelapp.tool.Utility;
import com.scnu.yxp.travelapp.tool.httpUtil;
import com.scnu.yxp.travelapp.view.MyScrollView;

public class HomePageActivity extends FragmentActivity {
	//定义滑动页面
	private ViewPager viewPager;
	//设置viewPager中的各个页面
	private View all_diaryPage, newPage, mePage;
	//设置viewPager的按钮
	private ImageView all_diaryBtn, newBtn, meBtn;
	//设置list存放三个页面
	private ArrayList<View> viewPageList ;
	//viewPage的适配器
	private PagerAdapter pagerAdapter;
	//按钮被按后的图片
	private int[] btn_pressed = new int[]{R.drawable.all_diary_pressed, R.drawable.new_btn_pressed,
			R.drawable.me_btn_pressed};
	//按钮被按后的图片
	private int[] btn_normal = new int[]{R.drawable.all_diary, R.drawable.new_btn,R.drawable.me_btn};
	//游记中的listview
	private ListView listView_homepage;
	//homepage的适配器
	private HomePageListViewAdapter adapter_homepage;
	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
	//homepage的回到顶部的按钮
	private ImageButton upBtn;
	//homepage的滑动view
	private MyScrollView myScrollView;
	//方便测量上滑的距离然后出现回到顶部的按钮
	private LinearLayout homepage_btn_window;
	//homepage的三个label按钮
	private ImageButton landscapeBtn, foodBtn, lifeBtn;
	//homepage的搜索按钮
	private ImageButton searchBtn;
	
	//new游记里的控件
	private ListView newListview;
	private SimpleAdapter newAdapter;
	private List<Map<String,String>> newItems;
	
	//me主页的
	private MyDiary myDairy;
	private MyCollection myCollection;
	private Button myDairyBtn, myCollectionBtn;
	private LinearLayout layout;
	private boolean isMyDairyShow = true;
	//判断是否连按两次退出
	private int isExit = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//防止在activity打开时edittext获取焦点
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN 
				| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
		setContentView(R.layout.activity_homepage);
		init();//实例化
		setListener();//设置事件监听
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
	}
	
	//实例化各个类
	public void init()
	{
		viewPager = (ViewPager)findViewById(R.id.viewPager);
		all_diaryBtn = (ImageView)findViewById(R.id.all_diary_btn);
		newBtn = (ImageView)findViewById(R.id.new_btn);
		meBtn = (ImageView)findViewById(R.id.me_btn);
		//通过layoutinflater载入界面
		LayoutInflater inflater = LayoutInflater.from(this);
		//最新游记的界面
		all_diaryPage = inflater.inflate(R.layout.homepage_all_diary, null);
		all_diaryBtn.setImageResource(btn_pressed[0]);
		listView_homepage = (ListView)all_diaryPage.findViewById(R.id.listView);
		upBtn = (ImageButton)all_diaryPage.findViewById(R.id.up_btn);
		myScrollView = (MyScrollView)all_diaryPage.findViewById(R.id.scrollView);
		upBtn.setVisibility(View.GONE);
		homepage_btn_window = (LinearLayout)all_diaryPage.findViewById(R.id.homepage_btn_window);
		foodBtn = (ImageButton)all_diaryPage.findViewById(R.id.food_btn);
		lifeBtn = (ImageButton)all_diaryPage.findViewById(R.id.life_btn);
		landscapeBtn = (ImageButton)all_diaryPage.findViewById(R.id.landscapte_btn);
		searchBtn = (ImageButton)all_diaryPage.findViewById(R.id.search_btn);
		//我的界面
		mePage = inflater.inflate(R.layout.homepage_me, null);
		layout = (LinearLayout)mePage.findViewById(R.id.fragmentLayout);
		myDairy = new MyDiary();
		myCollection = new MyCollection();
		myDairyBtn = (Button)mePage.findViewById(R.id.myDairyBtn);
		myCollectionBtn = (Button)mePage.findViewById(R.id.myCollectionBtn);
		//创建新游记
		newPage = inflater.inflate(R.layout.homepage_new, null);
		newListview = (ListView)newPage.findViewById(R.id.newlistView);
		newItems = new ArrayList<Map<String,String>>();
		getMyDairy();
		
		//初始化适配器
		for(int i = 0; i < 4; i++)
		{
			Map<String, String> listItem = new HashMap<String, String>();
			listItem.put(i+"", i+"");
			list.add(listItem);
		}
		adapter_homepage = new HomePageListViewAdapter(this, list);
		listView_homepage.setAdapter(adapter_homepage);
		//重新设置listview的高度
		Utility.setListViewHeightBasedOnChildren(listView_homepage);
		//通过list将三个界面加入viewPage中
		viewPageList = new ArrayList<View>();
		viewPageList.add(all_diaryPage);
		viewPageList.add(newPage);
		viewPageList.add(mePage);
		//设置viewpage的适配器
		pagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return viewPageList.size();
			}

			@Override
			public Object instantiateItem(ViewGroup container, int position) {
				((ViewPager)container).addView(viewPageList.get(position));
				return viewPageList.get(position);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				((ViewPager)container).removeView(viewPageList.get(position));
			}
			
		};
		viewPager.setAdapter(pagerAdapter);
		viewPager.setCurrentItem(0);
	}
	
	public void getMyDairy()
	{
		for(int i = 0 ; i < 2 ; i++)
		{
			HashMap<String, String> item = new HashMap<String, String>();
			if(i == 1)
			{
				item.put("text", "创建新游记");
			}else{
				item.put("text", "继续游记,巴黎游...");
			}
			
			newItems.add(item);
		}
		newAdapter = new SimpleAdapter(this,newItems,R.layout.homepage_new_footer,new String[]{"text"},
				new int[]{R.id.text});
		newListview.setAdapter(newAdapter);
	}
	
	
	//设置监听
	public void setListener()
	{
		onMyScrollListener listener = new onMyScrollListener();
		myScrollView.setOnScrollListener(listener);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {
				setBtnImage(index);
				if(index == 2)
				{
					getSupportFragmentManager().beginTransaction().replace(layout.getId(), myDairy).commit();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int index) {
				
			}
		});
		all_diaryBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewPager.setCurrentItem(0);
			}
		});
		newBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewPager.setCurrentItem(1);
			}
		});
		meBtn.setOnClickListener(new OnClickListener() {
		
			@Override
			public void onClick(View v) {
				viewPager.setCurrentItem(2);
				
			}
		});
		upBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//滚回到顶部
				myScrollView.scrollTo(10, 0);
				upBtn.setVisibility(View.GONE);
			}
		});
		landscapeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("item", "landscape");
				Intent intent = new Intent(HomePageActivity.this, HobbyActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		lifeBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("item", "life");
				Intent intent = new Intent(HomePageActivity.this, HobbyActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		foodBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("item", "food");
				Intent intent = new Intent(HomePageActivity.this, HobbyActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		searchBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(HomePageActivity.this, LocationSearchActivity.class);
				startActivity(intent);
			}
		});
		//me
		myDairyBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//设置点击后的样式
				if(!isMyDairyShow)
				{
					myDairyBtn.setBackgroundResource(R.drawable.me_blankframe);
					myDairyBtn.setTextColor(getResources().getColor(R.color.sky_blue));
					myCollectionBtn.setBackgroundResource(0);
					myCollectionBtn.setTextColor(Color.WHITE);
					isMyDairyShow = true;
					getSupportFragmentManager().beginTransaction().replace(layout.getId(), myDairy).commit();
				}
			}
		});
		myCollectionBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//设置点击后的样式
				if(isMyDairyShow)
				{
					myCollectionBtn.setBackgroundResource(R.drawable.me_blankframe);
					myCollectionBtn.setTextColor(getResources().getColor(R.color.sky_blue));
					myDairyBtn.setBackgroundResource(0);
					myDairyBtn.setTextColor(Color.WHITE);
					isMyDairyShow = false;
					getSupportFragmentManager().beginTransaction().replace(layout.getId(), myCollection).commit();
				}
			}
		});
		newListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				if(pos == 1)
				{
					Intent intent = new Intent(HomePageActivity.this, EditDairy.class);
					startActivity(intent);
				}else{
					Intent intent = new Intent(HomePageActivity.this, EditDairy.class);
					intent.putExtra("load", "load");
					startActivity(intent);
				}
			}
		});
	}
	
	//改变btn的图标
	public void setBtnImage(int index)
	{
		switch( index )
		{
			case 0:
				all_diaryBtn.setImageResource(btn_pressed[0]);
				newBtn.setImageResource(btn_normal[1]);
				meBtn.setImageResource(btn_normal[2]);
				break;
			case 1:
				all_diaryBtn.setImageResource(btn_normal[0]);
				newBtn.setImageResource(btn_pressed[1]);
				meBtn.setImageResource(btn_normal[2]);
				break;
			case 2:
				all_diaryBtn.setImageResource(btn_normal[0]);
				newBtn.setImageResource(btn_normal[1]);
				meBtn.setImageResource(btn_pressed[2]);
				break;
			default:
				break;
		}
	}
	
	//监听滑动距离然后使回到顶部按钮出现
	class onMyScrollListener implements com.scnu.yxp.travelapp.view.MyScrollView.OnScrollListener
	{

		@Override
		public void onScroll(int scrollY) {
			if(scrollY >= homepage_btn_window.getHeight())
			{
				upBtn.setVisibility(View.VISIBLE);
			}else{
				upBtn.setVisibility(View.GONE);
			}
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果按下返回键的响应事件，退出程序
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			isExit++;
			if(isExit == 2)
			{
				System.exit(0);
			}
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					isExit = 0;
				}
			});
			thread.start();
			Toast.makeText(HomePageActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
		}
		return true;
	}
	

	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.exit(0);
	}

}
